<template>
	<view class="page">

		<!-- 显示设备状态 -->
		<view class="machine-status" :class="statusClass(machineInfo.status)">
			{{ statusText(machineInfo.status) }}
		</view>

		<!-- 开关按钮 -->
		<view class="button-group">
			<view class="circle-btn open" :class="{active: machineInfo.status === 'open'}" @click="setStatus('open')">
				<text>开</text>
			</view>
			<view class="circle-btn close" :class="{active: machineInfo.status === 'close'}"
				@click="setStatus('close')">
				<text>关</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'

	const deviceId = ref(null)
	const machineInfo = ref({})

	function getDeviceInfo(deviceId) {
		return {
			name: '',
			status: 'close'
		};
	}

	onLoad(async (options) => {
		deviceId.value = options.id
		const deviceName = options.name

		// 设置页面标题
		uni.setNavigationBarTitle({
			title: deviceName
		})

		// 查询设备信息并赋值
		const res = await getDeviceInfo(deviceId)
		machineInfo.value = res
	})

	function setStatus(status) {
		debugger
		if (machineInfo.value.status === status) {
		    machineInfo.value.status = status === 'open' ? 'close' : 'open'
		  } else {
		    machineInfo.value.status = status
		  }
		// TODO: 在这里调用接口，传递 deviceId 和 status
		console.log(`水肥机${deviceId.value} 设置为 ${status}`)
	}

	function statusClass(status) {
		return status === 'open' ? 'status-open' : 'status-close'
	}

	function statusText(status) {
		return status === 'open' ? '运行中' : '已关闭'
	}
</script>

<style scoped lang="scss">
	.page {
		padding: 20rpx;
		background: #f5f5f5;
		min-height: 100vh;
		text-align: center;

		.machine-name {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.machine-status {
			font-size: 28rpx;
			margin-bottom: 30rpx;
			padding: 10rpx 20rpx;
			border-radius: 8rpx;
			display: inline-block;

			&.status-open {
				background: #4caf50;
				color: #fff;
			}

			&.status-close {
				background: #f44336;
				color: #fff;
			}
		}

		.button-group {
			display: flex;
			justify-content: center;
			gap: 40rpx;

			.circle-btn {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				font-weight: bold;
				color: #fff;
				opacity: 0.6;
				transition: all 0.3s;

				&.open {
					background: #4caf50;
				}

				&.close {
					background: #f44336;
				}

				&.active {
					opacity: 1;
					box-shadow: 0 0 12rpx rgba(0, 0, 0, 0.3);
					transform: scale(1.1);
				}
			}
		}
	}
</style>